package cn.com.bluemoon.daps.webhook.design;

import cn.com.bluemoon.daps.common.enums.StandardAuditStatus;
import cn.com.bluemoon.daps.common.exception.DapException;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;

/**
 * @author Jarod.Kong
 * @date 2021/1/12 8:40
 */
@Slf4j
public abstract class BaseAuditStatusChange implements AuditStatusChange {
    @Override
    public String change(String auditId, StandardAuditStatus targetAuditStatus,
                         Integer modifyTypeDefine, String nextStandardId) {
        if (targetAuditStatus.equals(auditStatus())) {
            Object sourceAudit = null;
            // 提供消费流程数据（当前流程状态->转为targetAuditStatus & 调整变更类型和更新标准状态
            Consumer<Void> consumer = processHandler(targetAuditStatus,
                    modifyTypeDefine,
                    sourceAudit);
        /*
        源数据状态 源审批状态
         */
            StandardAuditStatus sourceAuditStatus = StandardAuditStatus.草稿;
            if (sourceAuditStatus.equals(targetAuditStatus)) {
                log.warn("源审批状态[{}]与目标状态[{}]一致，不进行流程业务处理", sourceAuditStatus, targetAuditStatus);
                return null;
            }
            // sourceAudit consumer(targetAuditStatus、modifyType、sourceAudit) targetAuditStatus
            boolean needChange = changeStatus(sourceAudit, targetAuditStatus, modifyTypeDefine, nextStandardId);
            if (needChange) {
                consumer.accept(null);
            }else {
                throw new DapException("标准状态[" + sourceAuditStatus + "]不可转为状态[" + targetAuditStatus + "]");
            }
        }
        return nextStandardId;
    }

    public abstract boolean changeStatus(Object sourceAudit, StandardAuditStatus targetAuditStatus, Integer modifyTypeDefine, String nextStandardId);

    /**
     * 针对
     *
     * @param targetAuditStatus
     * @param modifyTypeDefine
     * @param standardAuditVersionMain
     * @return
     */
    private Consumer<Void> processHandler(StandardAuditStatus targetAuditStatus,
                                          Integer modifyTypeDefine,
                                          Object standardAuditVersionMain) {
        return v -> {

        };
    }

}
